;; generic/sequence.scm - (c) rohan drape, 2003-2005

;; Evaluates to the number of elements in the sequence `s'.

(define-generic len)
(define-method (len (s <List>))   (length s))
(define-method (len (s <Vector>)) (vector-length s))
(define-method (len (s <String>)) (string-length s))
(define-method (len (s <Stream>)) (stream-length s))

;; Evaluates to the `n'th element of the sequence `s'.

(define-generic elt)
(define-method (elt (s <List>)   (n <Int>)) (list-ref s n))
(define-method (elt (s <Vector>) (n <Int>)) (vector-ref s n))
(define-method (elt (s <String>) (n <Int>)) (string-ref s n))
(define-method (elt (s <Stream>) (n <Int>)) (stream-ref s n))

;; Mutates the `n'th element of the sequence `s' to `v'.

(define-generic elt!)
(define-method (elt! (s <List>)   (n <Int>) (v <Any>))  (list-set! s n v))
(define-method (elt! (s <Vector>) (n <Int>) (v <Any>))  (vector-set! s n v))
(define-method (elt! (s <String>) (n <Int>) (v <Char>)) (string-set! s n v))

;; Operate on the `n'th element from the right of the sequence `s'.

(define (elt-right s n)    (elt  s (- (- (len s) n) 1)))
(define (elt!-right s n v) (elt! s (- (- (len s) n) 1) v))

